/*
 * @Author: szx
 * @Date: 2022-03-15 15:10:32
 * @LastEditTime: 2022-03-15 15:20:29
 * @Description:
 * @FilePath: \leetcode\400-499\405\405.js
 */
/**
 * @param {number} num
 * @return {string}
 */
var toHex = function (num) {
    if (num === 0) {
        return '0';
    }
    const arr = [];
    for (let i = 7; i >= 0; i--) {
        const val = (num >> (4 * i)) & 0xf;
        if (arr.length > 0 || val > 0) {
            const digit = val < 10 ? String.fromCharCode('0'.charCodeAt() + val) : String.fromCharCode('a'.charCodeAt() + val - 10);
            arr.push(digit);
        }
    }
    return arr.join('');
};
